﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;

namespace EnterpriseFund.Controllers
{
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class SWJGManagerController : AdminBaseController
    {
        //
        // GET: /SWJGManage/
        /// <summary>
        /// 税务机构
        /// </summary>
        /// <returns>视图</returns>

        [AuthorityActionFilter("m_DM_SWJG", Order = 1)]
        [LogPageActionFilter("税务机构管理", Order = 2)]
        public ActionResult Index()
        {

            List<Model.DM_SWJG> codeNameList = new List<Model.DM_SWJG>();
            ViewData["blcode"] = true;
            BLL.DM_SWJG codeNameBll = new BLL.DM_SWJG();
            codeNameList = codeNameBll.GetModelList("");
            return View(codeNameList);
        }
        /// <summary>
        /// 编码树相关调用方法 主要用于刷新
        /// </summary>
        /// <returns>视图</returns>

        public ActionResult SWJGManageListPartial()
        {
            BLL.DM_SWJG codeNameBll = new BLL.DM_SWJG();
            List<Model.DM_SWJG> codeNameList = new List<Model.DM_SWJG>();
            codeNameList = codeNameBll.GetModelList("");
            ViewData["blcode"] = true;
            return PartialView("SWJGManageListPartial", codeNameList);
        }
        /// <summary>
        /// 编码增加修改
        /// </summary>
        /// <param name="codeNo">编码</param>
        /// <returns>视图</returns>
        public ActionResult DM_SWJGEdit(string SWJG_DM)
        {
            Model.DM_SWJG codeNameObj = new Model.DM_SWJG();
            ViewData["blcodeNo"] = true;
            BLL.TaxClass taxClassBLL = new BLL.TaxClass();
            ViewData["TaxClassList"] = taxClassBLL.GetModelList("");
            if (null != SWJG_DM)
            {
                SWJG_DM = Fetch.estimateString(SWJG_DM, CheckGetEnum.Safety);
                BLL.DM_SWJG codeNameBll = new BLL.DM_SWJG();
                codeNameObj = codeNameBll.GetModel(SWJG_DM);
                if (null == codeNameObj)
                {
                    codeNameObj = new Model.DM_SWJG();
                }
                else
                {
                    ViewData["blcodeNo"] = false;
                }
            }
            return View(codeNameObj);
        }
        /// <summary>
        /// 删除编码数据
        /// </summary>
        /// <param name="codeNo">编码编号</param>
        /// <returns>是否删除成功</returns>
        public string DelSWJG(string SWJG_DM)
        {
            string str = string.Empty;
            if (null != SWJG_DM)
            {
                SWJG_DM = Fetch.estimateString(SWJG_DM, CheckGetEnum.Safety);
                BLL.DM_SWJG codeNameBll = new BLL.DM_SWJG();
                try
                {
                    codeNameBll.Delete(SWJG_DM);
                    str = "ok";
                }
                catch (Exception)
                {
                    str = "error";
                }
            }
            return str;
        }
        /// <summary>
        /// 编码保存
        /// </summary>
        /// <param name="codeNo">编码编号</param>
        /// <param name="codeName">编码名字</param>
        /// <returns>是否修改成功</returns>
        /// 

        public ActionResult Save()
        {
            // string SWJG_DM = "";
            string str = "";

            var SWJG_DM = Request.Params["tbSWJG_DM"];
            var SWJG_MC = Request.Params["tbSWJG_MC"];
            var SWJG_JC = Request.Params["tbSWJG_JC"];
            var SWJG_BZ = Request.Params["tbSWJG_BZ"];
            var SJ_SWJG_DM = Request.Params["tbSJ_SWJG_DM"];
            var NSR_SWJG_BZ = Request.Params["tbNSR_SWJG_BZ"];
            var JGJC_DM = Request.Params["tbJGJC_DM"];
            var SWJG_JG = Request.Params["tbSWJG_JG"];
            var SWJG_YB = Request.Params["tbSWJG_YB"];
            var SWJG_DZ = Request.Params["tbSWJG_DZ"];
            var SWJG_DH = Request.Params["tbSWJG_DH"];
            var CZDH = Request.Params["tbCZDH"];
            var DYDZ = Request.Params["tbDYDZ"];
            var XZQH_DM = Request.Params["tbXZQH_DM"];
            var SWJG_FZR_DM = Request.Params["tbSWJG_FZR_DM"];
            var TS_NSRSBH = Request.Params["tbTS_NSRSBH"];
            var YXWS = Request.Params["tbYXWS"];
            var JBDM = Request.Params["tbJBDM"];
            var JCDM = Request.Params["tbJCDM"];
            var YXBZ = Request.Params["tbYXBZ"];
            Boolean isAdd = Convert.ToBoolean(Request.Params["isAdd"]);
            BLL.DM_SWJG codeNameBll = new BLL.DM_SWJG();
            if (isAdd)//增加时判断编码是否已存在
            {
                if (codeNameBll.Exists(SWJG_DM))
                {
                    str = "编号已存在";
                    Response.Redirect("~/SWJGManager/DM_SWJGEdit?mes=" + str);
                    return DM_SWJGEdit(SWJG_DM);
                }
            }

            Model.DM_SWJG codeNameObj = new Model.DM_SWJG();
            codeNameObj.SWJG_DM = SWJG_DM;
            codeNameObj.SWJG_MC = SWJG_MC;
            codeNameObj.SWJG_JC = SWJG_JC;
            codeNameObj.SWJG_BZ = (SWJG_BZ == "C") ? "J" : "B";
            codeNameObj.SJ_SWJG_DM = SJ_SWJG_DM;
            codeNameObj.NSR_SWJG_BZ = (NSR_SWJG_BZ == "C") ? "Y" : "N";
            codeNameObj.JGJC_DM = JGJC_DM;
            codeNameObj.SWJG_JG = SWJG_JG;
            codeNameObj.SWJG_YB = SWJG_YB;
            codeNameObj.SWJG_DZ = SWJG_DZ;
            codeNameObj.SWJG_DH = SWJG_DH;
            codeNameObj.CZDH = CZDH;
            codeNameObj.DYDZ = DYDZ;
            codeNameObj.XZQH_DM = XZQH_DM;
            codeNameObj.SWJG_FZR_DM = SWJG_FZR_DM;
            codeNameObj.TS_NSRSBH = TS_NSRSBH;
            codeNameObj.YXWS = (YXWS == "") ? 0 : Convert.ToInt32(YXWS);
            codeNameObj.JBDM = JBDM;
            codeNameObj.JCDM = JCDM;
            codeNameObj.YXBZ = (YXBZ == "C") ? "Y" : "N";

            try
            {
                if (isAdd)
                {
                    codeNameBll.Add(codeNameObj);
                }
                else
                {
                    codeNameBll.Update(codeNameObj);
                }
                str = "操作成功";

            }
            catch (Exception)
            {

                str = "操作错误";
            }

            //return str;
            Response.Redirect("~/SWJGManager/DM_SWJGEdit?mes=" + str);
            return DM_SWJGEdit(SWJG_DM);
        }
    }
}
